package com.wx.mp.controller;

import com.wx.mp.dao.TCarCityDao;
import com.wx.mp.domain.TCarCity;
import com.wx.mp.request.city.oper.RefreshCarCityReq;
import com.wx.mp.request.city.query.TCarCityReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: CarCityController.java
 * @Description:
 * @version: v1.0.0
 * @author: wangfs
 * @date: 2019-01-11 13:26
 * Modification History:
 * Date             Author          Version            Description
 * ---------------------------------------------------------*
 * 2019-01-11                   v1.0.0               创建
 */
@Api(value = "出发地目的地", description = "出发地目的地")
@RestController
@RequestMapping("/wx/carCity")
public class CarCityController {

    @Autowired
    private TCarCityDao TCarCityDao;

    @ApiOperation("出发地目的地")
    @PostMapping("/queryCarCityByType")
    public List<TCarCity> queryCarCityByType(@RequestBody TCarCityReq carCityReq){
        List<TCarCity> resultList = new ArrayList<>();
        List<TCarCity> list = TCarCityDao.queryCarCityByType(carCityReq);
        if(!CollectionUtils.isEmpty(list)){
            if(null != carCityReq && carCityReq.getType() != null){
                resultList = list.stream().filter(s -> s.getType().equals(carCityReq.getType())).collect(Collectors.toList());
            }
        }

        return resultList;
    }

    @ApiOperation("刷新缓存-出发地目的地")
    @PostMapping("/refreshCarCity")
    public String refreshCarCity(@RequestBody RefreshCarCityReq refreshCarCityReq){
        if(refreshCarCityReq == null || StringUtils.isEmpty(refreshCarCityReq.getToken())) {
            return "token串不能为空!";
        }else  if(!refreshCarCityReq.getToken().equals("7D565A03919E413AB86912097EAC5416")){//做个验证，防止乱刷新
            return "token串不正确!";
        }else{
            return TCarCityDao.refreshCarCity(refreshCarCityReq);
        }


    }
}
